package com.ybkj.daijia.server.event.listener;


import com.ybkj.daijia.server.amqp.rabbitTemplate.PushTemplate;
import com.ybkj.daijia.server.event.model.DriverTimeEvent;
import com.ybkj.daijia.server.model.Driver;
import com.ybkj.daijia.server.service.DriverService;
import com.ybkj.daijia.server.service.DriverTimeService;
import com.ybkj.daijia.server.service.ZhuancheService;
import com.ybkj.daijia.server.sp.TjyyMessage;
import com.ybkj.daijia.server.zhuan.Car;
import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

@Component
public class DriverTimeEventListener implements ApplicationListener<DriverTimeEvent> {

    @Autowired
    PushTemplate pushTemplate;
    @Autowired
    private DriverTimeService driverTimeService;
    @Autowired
    private DriverService driverService;
    @Autowired
    private ZhuancheService zhuancheService;

    @Override
    @Async
    public void onApplicationEvent(DriverTimeEvent event) {

        String type = (String) event.getSource();
        final Driver driver = event.getDriver();

        Calendar calendar = Calendar.getInstance();

        if (null != driver) {
            if ("online".equals(type)) {

                driverTimeService.driverTimeOnline(driver, calendar);

            } else if ("offline".equals(type)) {

                driverTimeService.driverTimeOffline(driver, calendar);
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        Calendar instance = Calendar.getInstance();
                        TjyyMessage tjyy = new TjyyMessage();
                        tjyy.setDriverIdCode(driver.getIdCard());
                        tjyy.setDriverName(driver.getRealname());

                        Car car = zhuancheService.findCar(driver.getCarId());
                        String type = "无";

                        if (car != null) {
                            type = car.getCarTypeName();
                        }
                        String carNum = "无";
                        if (car != null) {
                            carNum = car.getCarNumber();
                        }
                        tjyy.setVehicleType(type);
                        tjyy.setVehicleLicense(carNum);
                        Date lastTime = driverTimeService
                            .getDriverLastOnlineTime(driver.getId(), instance);
                        tjyy.setOnlineTime(lastTime);
                        Date end = new Date();
                        tjyy.setOfflineTime(end);
                        Map<String, Object> stringObjectMap = zhuancheService
                            .countOnDutyByDriver(driver.getId(), lastTime, end);
                        int i = 0;
                        Object object = stringObjectMap.get("operateSum");
                        if (object != null) {
                            i = ((Double) stringObjectMap.get("operateSum")).intValue();
                        }
                        tjyy.setOperateSum(i);
                        tjyy.setPassengerMilSum(
                            new BigDecimal((Double) stringObjectMap.get("passengerMilSum"))
                                .setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
                        tjyy.setPassengerTimeSum((Double) stringObjectMap.get("passengerTimeSum"));
                        //tjyy.setNoneMislSum(new BigDecimal((Double) stringObjectMap.get("noneMislSum")).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
                        tjyy.setNoneMilSum(
                            new BigDecimal((Double) stringObjectMap.get("noneMislSum"))
                                .setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
                        tjyy.setPaidSum(new BigDecimal((Double) stringObjectMap.get("paidSum"))
                            .setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
                        tjyy.setReceivableSum(
                            new BigDecimal((Double) stringObjectMap.get("receivableSum"))
                                .setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
                        tjyy.setDriverTotalIncome(
                            new BigDecimal((Double) stringObjectMap.get("driverTotalIncome"))
                                .setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
                        tjyy.setWaitMilSum(
                            new BigDecimal((Double) stringObjectMap.get("waitMilSum"))
                                .setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());

                        //推送监管平台
                        pushTemplate.sendMessage(tjyy);
                    }
                }).start();

            }
        }


    }

}
